# To add a new cell, type '# %%'
# To add a new markdown cell, type '# %% [markdown]'
# %% [markdown]
# # 插入一个新作文
# %% [markdown]
# 自定义一个HASH

# %%
hash=input()

# %% [markdown]
# 从下方找到您的作文对应的序号

# %%
import os
docx_File_List=[]
for i in os.listdir(os.getcwd()):
    if(i[-5:]==".docx"):
        docx_File_List.append(i)
for i in range(0,len(docx_File_List)):
    print(str(i)+" - "+docx_File_List[i])

# %% [markdown]
# 输入作文地址（序号，1个数字）

# %%
index=int(input())
print("您选择了：【"+docx_File_List[index]+"】")


# %%
import json
import time
try:
    file=open("index.json","r",encoding='utf-8')
    filejs=json.load(file)
    file.close()
except IOError:
    filejs={"data":{}}

filejs["data"][hash]={"origin":"/articlelist/"+hash+".json","timestamp":int(time.time())}

fileout=json.dumps(filejs,sort_keys=True,indent=4,ensure_ascii=False)
file=open("index.json","w",encoding='utf-8')
file.write(fileout)
file.close()

import datetime
from docx import Document

doc=Document(docx_File_List[index])

currentline=0
print("请确认，【"+doc.paragraphs[currentline].text+"】是标题，（Y-标题/N-正文第一行）")
if (input("Headline?(Y/N)")=="Y"):
    headline=doc.paragraphs[currentline].text
    print("headline="+doc.paragraphs[currentline].text)
    currentline=currentline+1
print("请确认，【"+doc.paragraphs[currentline].text+"】是作者栏，（Y-作者栏/N-正文）")
if(input("Author?(Y/N)")=="Y"):
        currentline=currentline+1

data__zpackage_article=""
for cl in range(currentline,len(doc.paragraphs)):
    data__zpackage_article=data__zpackage_article+"<p>"+doc.paragraphs[cl].text+"</p>"

print("请确认，以下是正文，（Y-正文/N-手动操作）\n")
print(data__zpackage_article)
if(input("请确认，以下是正文，（Y-正文/N-手动操作）：")=="Y"):
    pass

fiction_material=False
if(input("素材是否虚构（Y/N）：")=="Y"):
    fiction_material=True

file=open(hash+".json","w",encoding='utf-8')
filejs["data"]={
    "hash":hash,
    "headline":headline,
    "body":"/articlelist/"+hash+".zpackage_article",
    "datePublished":datetime.datetime.now().strftime('%Y-%m-%d'),
    "datePublishedtime":"",
    "dateModified":datetime.datetime.now().strftime('%Y-%m-%d'),
    "dateModifiedtime":"",
    "fictionmaterial":fiction_material,
    "originwordexist":"/articlelist/"+docx_File_List[index],
    "handwrittenexist":False,
    }
fileout=json.dumps(filejs,sort_keys=True,indent=4,ensure_ascii=False)
file.write(fileout)
file.close()

file=open(hash+".zpackage_article","w",encoding='utf-8')
file.write(data__zpackage_article)
file.close()


# %%



